greatcfandomcom-20200214-history
Introduction to Kornshell
Kornshell See http://docstore.mik.ua/orelly/unix/ksh/index.htm Which Version There are two main versinos of the Korn Shell, 1988 and 1993. Finding the Version in ksh 1998 $/usr/ccs/bin/what /bin/ksh /bin/ksh: Version M-11/16/88i SunOS 5.10 Generic 128342-01 Jan 2008 Or * ESC, Ctrl+v Customising the Shell's Behaviour To find out current settings use: $ set -o allexport off braceexpand on emacs on errexit off errtrace off functrace off hashall on histexpand on history on ignoreeof off interactive-comments on keyword off monitor on noclobber off noexec off noglob off nolog off notify off nounset off onecmd off physical off pipefail off posix off privileged off verbose off vi off xtrace off To raise an error when variable are null use: $ set -o nounset Shell Scripts To get the script interpreted as a korn shell script include the hash-bang command: #!/usr/bin/ksh . . . Including Other Scripts Scripts can be included as follows: #!/usr/bin/ksh . ./script.sh Variables var=value echo $var Evaluating an Expressions Aritmetic expressions can be used in several different ways. $ echo $(( 1 + 3 )) 4 Using variables in expressions. $ x=12 $ y=15 $ echo $(( $x + $y )) 27 Evaluating a string variable. $exp="1 + 2" $echo $(( $exp )) 3 Special Variables * $? - exit status * $0 - command procedure * $# - number of positional parameters * $$ - process number of shell * $- - current shell flags $0 - Exit Status The following interactive session shows the way th exit status is set. If a command executes successfully 0 will be returned as the exit status. If the command does not execute successfully the exit code will be non-zero (127 in this case). $ echo hello hello $ echo $? 0 $ cho hello ksh: cho: not found $ echo $? 127 $ Escaping and Delayed Evaluation The $ character can be escaped by using "\$". For example: $ X=1 $ echo $X 1 $ Y="\${X}" $ echo ${Y} ${X} $ eval echo ${Y} 1 $ Comparing String with Output from Commands When comparing the output of commands with text becareful of the behaviour of echo. echo "X Y Z" > expected.tmp echo $( . . . ) > actual.tmp Though expected.tmp and actual.tmp may be the same a comparison in a script may not be. if "X Y Z" "$( . . . )" then fi Use: EXPECTED =$( echo "X Y Z" ) ACTUAL=$( . . . ) ACTUAL=$( echo ${ACTUAL} ) if "${EXPECTED}" "${ACTUAL}" then fi for Loops Syntax for a fow loop. for name list do statements that can use $name... done Example For a source file, resources.txt: doc/Library/A.AH013/dd.txt doc/Library/Gendocs/ex012_001.txt doc/Library/Gendocs/ex013_001.txt doc/Library/Gendocs/ex015_001.txt doc/Library/Gendocs/ex016_001.txt doc/Resource/A.AA004/doc.sty Check that each file exists: for f $( cat resources.txt ) do ls $f done while Loops #!/bin/ksh while read -r line do print -r -- "$line" done Ask the User to Continue #!/bin/ksh PROMPT="Continue (Y/N)?" echo $PROMPT while read -r REPLY do if [ $REPLY = "N" ] then break fi echo $PROMPT done A variation. PROMPT="Continue (Y)" echo $PROMPT while read ANS do case $ANS in Y) echo "YES" break;; *) echo $PROMPT continue esac done getopts - parsing the command line The standard way to handle input on the command line is to use the getopts() function. The following example is from the manual entry for getopts. #!/usr/bin/ksh aflag= bflag= while getopts ab: name do case $name in a) aflag=1;; b) bflag=1 bval="$OPTARG";; ?) printf "Usage: %s: -a value args\n" $0 exit 2;; esac done if [ ! -z "$aflag" ]; then printf "Option -a specified\n" fi if [ ! -z "$bflag" ]; then printf 'Option -b "%s" specified\n' "$bval" fi shift $(($OPTIND - 1)) printf "Remaining arguments are: %s\n" "$*" Select Select without prompt. select REPLY in Start Continue Stop Exit do echo You chose: $REPLY if "$REPLY" = "Exit" then break; fi done Running the example: $./select.ksh 1) Start 2) Continue 3) Stop 4) Exit #? 1 You chose: Start #? 2 You chose: Continue #? 3 You chose: Stop #? 4 You chose: Exit $ Arrays A simple array. Not how the incrementiong of $i is done. #!/bin/ksh release0="REL-20100101-XXX-A" release1="REL-20100102-YYY-A" release2="REL-20100102-ZZZ-A" i=0 while [ $i -lt ${#release*} ] do echo Element i = $i echo ${releasei} i=$(( $i + 1 )) done Running the example: $./array.ksh Element i = 0 REL-20100101-XXX-A Element i = 1 REL-20100102-YYY-A Element i = 2 REL-20100102-ZZZ-A $ Functions #!/usr/bin/ksh function reverseargs { echo "$2, $1" } reverseargs world hello Function Scope Do variables declared at the top level of a script carry over into function bodies when the functions are called. The following script suggests that they do. #!/usr/bin/ksh X="Hello" function echoX { echo $X } echoX Run the script. $./scopetest.ksh Hello $ This also works with the alternative way of defining functions. #!/usr/bin/ksh X="Hello" echoX() { echo $X } echoX Parameter Expansion - Substrings ${parameter#pattern} Remove small left pattern. $ print ${PWD} /home/cozensj/korn $ print ${PWD#/} home/cozensj/korn $ print ${PWD#/*/} cozensj/korn $ ${parameter##pattern} $ print ${PWD} /home/cozensj/korn $ print ${PWD##/} home/cozensj/korn $ print ${PWD##/*/} korn $ ${parameter%pattern} $print ${PWD%/*} /home/cozensj ${parameter%%pattern} $print ${PWD%%/*} $ ${parameter:offset:length} and ${parameter:offset} Version > 11/16/88. ${parameter/pattern/string}, ${parameter/#pattern/string}, ${parameter/%pattern/string}, ${parameter//pattern/string} Version > 11/16/88. Commands alias alias -tx [ name [ = value]...] unalias name... unalias -a Alias can be used to abbrivated other commands: $ ll="ls -l" $ alias . . . ll='ls -l' . . . export Shell built-in function part of the set, unset, setenv, unsetenv, export group. r $ history . . . 122 chomd +x f1 123 ls 124 cd .. $ r 123 ls return $ f() edit $ { edit $ return 3 edit $ } $ f $ echo $? 3 $ set Shell built-in function part of the set, unset, setenv, unsetenv, export group. setenv Shell built-in function part of the set, unset, setenv, unsetenv, export group. unset Shell built-in function part of the set, unset, setenv, unsetenv, export group. unsetenv Shell built-in function part of the set, unset, setenv, unsetenv, export group. wait wc what whence while who Category:Introduction